from django.contrib import admin
from django.conf import settings
from django import forms
from models import Page, Client, Category, Award, Task, Project, Picture
from prettypony.widgets import AdminImageFieldWithThumbWidget

class TaskAdmin(admin.ModelAdmin):
    list_display = ('__unicode__', 'number_of_projects',)
    prepopulated_fields = {'slug': ('title',)}
    exclude = ('order',)
    
    def number_of_projects(self, obj):
        return "%s" % (obj.project_set.count())

class CategoryAdmin(admin.ModelAdmin):
    prepopulated_fields = {'slug': ('title',)}

class AwardAdmin(admin.ModelAdmin):
    prepopulated_fields = {'slug': ('title',)}

class PageAdmin(admin.ModelAdmin):
    list_display = ('title', 'public',)
    prepopulated_fields = {'slug': ('title',)}
    fieldsets = (
        (None, {
            'fields': ('title', 'public',)
        }),
        ('Extra', {
            'classes': ('collapse',),
            'fields': ('slug', 'description', 'tags',)
        }),
    )

class PageForm(forms.ModelForm):
    """
    Orderable inlines using drag and drop with jQuery UI
    http://www.djangosnippets.org/snippets/1053/
    """
    model = Page
    class Media:
        js = (
            settings.MEDIA_URL + 'js/jquery-1.2.6.pack.js',
            settings.MEDIA_URL + 'js/ui.core.packed.js',
            settings.MEDIA_URL + 'js/ui.sortable.packed.js',
            settings.MEDIA_URL + 'js/menu-sort.js',
        )

class ClientAdmin(admin.ModelAdmin):
    list_display = ('name', 'url', 'slug',)
    prepopulated_fields = {'slug': ('name',)}
    fieldsets = (
        (None, {
            'fields': ('name', 'slug',)
        }),
        ('Extra', {
            'classes': ('collapse',),
            'fields': ('url', 'description',)
        }),
    )

class ProjectAdmin(admin.ModelAdmin):
    list_display = ('title', 'client', 'url', 'public', 'page',)
    list_filter = ('client',)
    prepopulated_fields = {'slug': ('title',)}
    # fields = ('page', 'client', 'title', 'slug', 'url', 'description', 'roles', 'categories', 'awards', 'tags', 'launch', 'public',)
    exclude = ('order',)

class ProjectInline(admin.StackedInline):
    model = Project
    extra = 1
    prepopulated_fields = {'slug': ('title',)}
    fieldsets = (
        (None, {
            'fields': ('client', 'title', 'order',)
        }),
        ('Extra', {
            'classes': ('collapse',),
            'fields': ('slug', 'tags',)
        }),
    )

class ProjectForm(forms.ModelForm):
    """
    Orderable inlines using drag and drop with jQuery UI
    http://www.djangosnippets.org/snippets/1053/
    """
    model = Project
    class Media:
        js = (
            settings.MEDIA_URL + 'js/jquery-1.2.6.pack.js',
            settings.MEDIA_URL + 'js/ui.core.packed.js',
            settings.MEDIA_URL + 'js/ui.sortable.packed.js',
            settings.MEDIA_URL + 'js/menu-sort.js',
        )

class PictureAdmin(admin.ModelAdmin):
    list_display = ('thumbnail_and_caption', 'project',)
    list_filter = ('project',)
    prepopulated_fields = {'slug': ('caption',)}
    exclude = ('order',)

    def formfield_for_dbfield(self, db_field, **kwargs):
        field = super(PictureAdmin,self).formfield_for_dbfield(db_field,**kwargs)
        if db_field.name == 'image':
           return forms.ImageField(widget=AdminImageFieldWithThumbWidget())
        return field
    
    def thumbnail_and_caption(self, obj):
        """Show the automatic sorl-thumbnail"""
        if(obj.caption):
            return ("%s<br />%s" % (obj.image.thumbnail_tag, obj.caption,))
        else:
            return ("%s" % (obj.image.thumbnail_tag,))
    thumbnail_and_caption.allow_tags = True

class PictureInline(admin.StackedInline):
    model = Picture
    extra = 1
    prepopulated_fields = {'slug': ('caption',)}
    fieldsets = (
        (None, {
            'fields': ('image', 'order',)
        }),
        ('Caption, Slug, and Tags', {
            'classes': ('collapse',),
            'fields': ('caption', 'slug', 'tags',)
        }),
    )
    # How to display image from ImageField in ModelAdmin:
    # http://groups.google.com/group/django-users/browse_thread/thread/7cfbcc67f3af02b8/0fa00367ff96f8c8
    def formfield_for_dbfield(self, db_field, **kwargs):
        field = super(PictureInline,self).formfield_for_dbfield(db_field,**kwargs)
        if db_field.name == 'image':
           return forms.ImageField(widget=AdminImageFieldWithThumbWidget())
        return field

admin.site.register(Page, PageAdmin,
    inlines = [ProjectInline],
    form = PageForm,
)
admin.site.register(Client, ClientAdmin)
admin.site.register(Category, CategoryAdmin)
admin.site.register(Award, AwardAdmin)
admin.site.register(Task, TaskAdmin)
admin.site.register(Project, ProjectAdmin,
    inlines = [PictureInline],
    form = ProjectForm,
)
# admin.site.register(Picture, PictureAdmin)
